<template xmlns="http://www.w3.org/1999/html">
  <el-form :rules="rules" class="forgetPwd-container" label-position="left"
           label-width="0px" v-loading="loading">
    <div style="text-align: left; margin-bottom: 15px">
      <el-link type="primary" href="/" :underline=false>返回登录页面</el-link>
    </div>
    <h3 class="forgetPwd-title">忘记密码</h3>
    <el-form-item prop="email">
      <el-input type="text" v-model="forgetPwdForm.email"
                auto-complete="off" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item prop="account">
      <el-input type="text" v-model="forgetPwdForm.username"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="forgetPwdForm.password"
                auto-complete="off" placeholder="新密码"></el-input>
    </el-form-item>
    <el-form-item prop="confirmPass">
      <el-input type="password" v-model="forgetPwdForm.confirmPwd"
                auto-complete="off" placeholder="确认新密码"></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%" @click="forgetPwd">提交密码修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'ForgetPwd',
  data () {
    return {
      rules: {
        email: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
        account: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        checkPass: [{required: true, message: '请输入新密码', trigger: 'blur'}],
        confirmPass: [{required: true, message: '请再次输入新密码', trigger: 'blur'}]
      },
      forgetPwdForm: {
        email: '',
        username: '',
        password: '',
        confirmPwd: ''
      },
      loading: false
    }
  },
  methods: {
    forgetPwd () {
      var _this = this
      if (this.forgetPwdForm.email == null || this.forgetPwdForm.email == '') {
        this.$message('邮箱不能为空')
        return false
      }
      if (
        this.forgetPwdForm.username == null ||
          this.forgetPwdForm.username == ''
      ) {
        this.$message('账号不能为空')
        return false
      }
      if (
        this.forgetPwdForm.password == null ||
          this.forgetPwdForm.password == '' ||
          this.forgetPwdForm.confirmPwd == null ||
          this.forgetPwdForm.confirmPwd == ''
      ) {
        this.$message('密码不能为空')
        return false
      }
      if (!(this.forgetPwdForm.password === this.forgetPwdForm.confirmPwd)) {
        this.$message('两次密码输入不一致')
        return false
      }
      this.loading = true
      this.postRequestByJSON('/forgetPwd',
        {
          username: this.forgetPwdForm.username,
          newPassword: this.forgetPwdForm.password,
          email: this.forgetPwdForm.email
        }
      ).then(resp => {
        _this.loading = false
        var data = resp.data
        if (resp && resp.status == 200) {
          this.$message(data.msg)
          _this.$router.push('/login')
        } else {
          this.$message(data.msg)
        }
      })
    }
  },
  watch: {
    'forgetPwdForm.email': function (val) {
      if (val != null && val != '') {
        this.rules.email[0].required = false
      } else {
        this.rules.email[0].required = true
      }
    },
    'forgetPwdForm.username': function (val) {
      if (val != null && val != '') {
        this.rules.account[0].required = false
      } else {
        this.rules.account[0].required = true
      }
    },
    'forgetPwdForm.password': function (val) {
      if (val != null && val != '') {
        this.rules.checkPass[0].required = false
      } else {
        this.rules.checkPass[0].required = true
      }
    },
    'forgetPwdForm.confirmPwd': function (val) {
      if (val != null && val != '') {
        this.rules.confirmPass[0].required = false
      } else {
        this.rules.confirmPass[0].required = true
      }
    }
  }
}
</script>

<style scoped>
  .forgetPwd-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 50px auto;
    width: 350px;
    padding: 20px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .forgetPwd-title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #505458;
  }

</style>
